/**
 * 筛选的最大月份
 */
const scrrenMaxMonth = 3;

import st from "@/utils/utils/showtoast.js";
import stoarge from "@/utils/utils/stoarge.js";
import { cancelOrder, deleteOrder, repayOrder } from "@/API/API/order.js";

export default {
	/**
	 * 判断是否登录
	 */
	isLogin() {
		if (stoarge.getToken() === "") {
			uni.navigateTo({
				url: `/pages/login/login`,
			});
			return false;
		}
		return true;
	},
	/**
	 * 取消订单
	 */
	onCancelOrder(orderID, handler) {
		uni.showModal({
			title: "提示",
			content: "确定要取消该订单吗？",
			success: (res) => {
				if (res.confirm) {
					cancelOrder(orderID).then((data) => {
						st.success("取消订单成功", () => {
							handler();
						});
					});
				}
			},
		});
	},
	/**
	 * 删除订单
	 */
	onDeleteOrder(orderID, handler) {
		uni.showModal({
			title: "提示",
			content: "确定要删除该订单吗？",
			success: (res) => {
				if (res.confirm) {
					deleteOrder(orderID).then((data) => {
						st.success("删除订单成功", () => {
							handler();
						});
					});
				}
			},
		});
	},
	/**
	 * 去支付
	 */
	onPayOrder(orderID, handler) {
		let methods = this.getPlatform();
		repayOrder(orderID, methods).then((res) => {
			this.onWeixinPay(res, handler);
		});
	},
	/**
	 * 微信支付
	 */
	onWeixinPay(res, handler) {
		//微信支付
		WeixinJSBridge.invoke("getBrandWCPayRequest", Object.assign(res.data), (result) => {
			if (result.err_msg == "get_brand_wcpay_request:ok") {
				st.success("支付成功", () => {
					handler("success");
				});
			} else {
				st.show("支付失败", () => {
					uni.redirectTo({ url: '/subPages/technician/tabBar/orderList' });
					return;
				});
			}
		});
	},
	/**
	 * 获取当前应用所在平台
	 */
	getPlatform() {
		let methods = "";
		// #ifdef APP-PLUS
		methods = "app";
		// #endif
		// #ifdef H5
		methods = "h5";
		// #endif
		// #ifdef MP-WEIXIN
		methods = "miniapp";
		// #endif

		return methods;
	},
	/**
	 * 获取昵称的第一个字
	 */
	getFirstLetter(name) {
		let array = name.split("");
		return array[0];
	},
	/**
	 * rpx转px 返回的数值
	 */
	rpxToPx(distance) {
		const screenWidth = uni.getSystemInfoSync().windowWidth;
		return ((screenWidth / 750) * distance).toFixed(2);
	},
	/**
	 * px转rpx
	 */
	pxToRpx(distance) {
		const screenWidth = uni.getSystemInfoSync().windowWidth;
		return ((750 / screenWidth) * distance).toFixed(2).toString() + "rpx";
	},
	/**
	 * 获取当前时间的年月日星期及之后一周的日期
	 */
	getCurrentDateWeekAndAfter() {
		let dateArray = [];
		const date = new Date(); //获取当前时间
		const dateTime = date.getTime(); //今天的时间戳
		const weekArray = ["周日", "周一", "周二", "周三", "周四", "周五", "周六"];

		for (let i = 0; i < 7; i++) {
			let current = dateTime + 24 * 60 * 60 * 1000 * i; //往后推一天的时间戳
			let dateObj = new Date(current); //根据时间戳生成日期对象
			let year = dateObj.getFullYear(); //年
			let month = (dateObj.getMonth() + 1).toString().padStart(2, "0"); //月
			let day = dateObj.getDate().toString().padStart(2, "0"); //日
			let week = weekArray[dateObj.getDay()]; //星期
			dateArray.push({
				title: month + "-" + day,
				total: year + "-" + month + "-" + day,
				week: week,
				select: i === 0,
			});
		}
		return dateArray;
	},

	/**
	 * 获取当前日期
	 */
	getCurrentDate() {
		const date = new Date();
		const year = date.getFullYear();
		const month = (date.getMonth() + 1).toString().padStart(2, "0");
		const day = date.getDate().toString().padStart(2, "0");

		return year + "-" + month + "-" + day;
	},

	/**
	 * 根据时间获取做大选择的年月日
	 */
	getEndDate(startDate) {
		if (startDate === "") {
			return "";
		} else {
			const dateArray = startDate.split("-");
			const year = parseInt(dateArray[0]);
			const month = parseInt(dateArray[1]);
			const day = parseInt(dateArray[2]);

			let date = "";
			if (month + scrrenMaxMonth > 12) {
				date =
					(year + 1).toString() +
					"-" +
					(month + scrrenMaxMonth - 12).toString().padStart(2, "0") +
					"-" +
					day.toString().padStart(2, "0");
			} else {
				date =
					year.toString() +
					"-" +
					(month + scrrenMaxMonth).toString().padStart(2, "0") +
					"-" +
					day.toString().padStart(2, "0");
			}
			return date;
		}
	},
	/**
	 * 根据时间获取做小选择的年月日
	 */
	getStartDate(endDate) {
		if (endDate === "") {
			return "";
		} else {
			const dateArray = endDate.split("-");
			const year = parseInt(dateArray[0]);
			const month = parseInt(dateArray[1]);
			const day = parseInt(dateArray[2]);

			let date = "";
			if (month - scrrenMaxMonth < 1) {
				date =
					(year - 1).toString() +
					"-" +
					(12 + month - scrrenMaxMonth).toString().padStart(2, "0") +
					"-" +
					day.toString().padStart(2, "0");
			} else {
				date =
					year.toString() +
					"-" +
					(month - scrrenMaxMonth).toString().padStart(2, "0") +
					"-" +
					day.toString().padStart(2, "0");
			}

			return date;
		}
	},

	/**
	 * 获取出行方式
	 */
	getTravelTypeText(type) {
		let text = "";

		const index = parseInt(type) - 1;
		const travelTypeArray = ["出租车", "地铁/公交"];

		if (index >= 0 && index < travelTypeArray.length) {
			text = travelTypeArray[index];
		}

		return text;
	},

	/**
	 * 预览图片
	 * @param {Array} imageArray 图片数组
	 * @param {Number} current 当前图片索引
	 */
	onPreviewImage(imageArray, current) {
		uni.previewImage({
			urls: imageArray,
			current: current,
		});
	},
};
